#ifndef ITYW_CMTD_TCP_COMMON_H
#define ITYW_CMTD_TCP_COMMON_H

#define INTERNAL_VERSION 1

#ifndef NULL 
#define NULL 0
#endif 

#include <string.h>

namespace cmtd
{
	//enum MessageType
	//{
	//	MSG_TYPE_DEFAULT = 0,
	//	MSG_TYPE_CMD_REGISTER_RESPONSE,
	//	MSG_TYPE_CMD_FETCH_RESPONSE,
	//	MSG_TYPE_CMD_NOTIFY,
	//	MSG_TYPE_CMD_FINISH_RESPONSE,
	//	MSG_TYPE_CMD_UNREGISTER_RESPONSE,
	//	MSG_TYPE_CMD_POST_PSH,
	//};

	enum MemoBusCommandType
	{
		CMD_UNDEFINED = 0,
		CMD_REGISTER_REQ = 1,
		CMD_REGISTER_RESP = 2,
		CMD_UNREGISTER_REQ = 3,
		CMD_UNREGISTER_RESP = 4,
		CMD_FETCH_REQ = 5,
		CMD_FETCH_RESP = 6,
		CMD_FINISH_REQ = 7,
		CMD_FINISH_RESP = 8,
		CMD_POST_PSH = 9,
		CMD_NOTIFY_PSH = 10,
		CMD_KEEPALIVE = 11,
		CMD_ONLINE = 12,
		CMD_OFFLINE = 13,
	};

	enum PushType
	{
		PSHT_DEFAULT = 0,
		PSHT_FETCH_RESP = 1,
		PSHT_POST_DATA = 2,
		PSHT_NOTIFY = 3,
	};

	struct IOCP_RECV_BUFFER
	{
		unsigned char * pBuffer;
		unsigned long ulBufferRecvLen;
		unsigned long ulBufferRecvOffset;
		unsigned long ulBufferRecvSerialNo;
		unsigned long ulBufferRecvTimestamp;
		bool bRecvNeedContinue;
	};

	typedef IOCP_RECV_BUFFER * IOCP_RECV_BUFFER_PTR;

	struct MESSAGE_INFO
	{
		int nMsgType;
		int nResult;
		char szSession[64];
		int msgDataLen;
		unsigned char * msgData;
		void * userData;
		MESSAGE_INFO()
		{
			nMsgType = 0;
			nResult = 0;
			memset(szSession, 0, 64);
			msgDataLen = 0;
			msgData = NULL;
			userData = NULL;
		}
		~MESSAGE_INFO() {}
	};

	typedef MESSAGE_INFO *MESSAGE_INFO_PTR;

	typedef void(__stdcall *fMessageCallback)(MESSAGE_INFO);

	struct SERVER_INFO
	{
		char szServerIp[32];
		unsigned int uiServerPort;
		SERVER_INFO()
		{
			memset(szServerIp, 0, 32);
			uiServerPort = 0;
		}
		~SERVER_INFO(){}
	};

	typedef SERVER_INFO *SERVER_INFO_PTR;	

	struct MEMOBUS_HEAD
	{
		unsigned char ucMark[4];
		unsigned int uiVersion : 16;
		unsigned int uiCmdType : 16;
		unsigned long ulCmdLen;
		unsigned long ulSerialNo;
		unsigned long ulTimeStamp;
		MEMOBUS_HEAD()
		{
			uiVersion = INTERNAL_VERSION;
			uiCmdType = cmtd::CMD_UNDEFINED;
			ulCmdLen = 0;
			ulSerialNo = 0;
			ulTimeStamp = 0;
		}
		~MEMOBUS_HEAD() {}
	};

#define MAKE_HEAD(x) { x.ucMark[0] = 'C'; x.ucMark[1] = 'M'; x.ucMark[2] = 'T'; \
 x.ucMark[3] = 'D';} 

	struct REGISTER_PARAMETER
	{
		int nType;
		int nStatus;
		int nDealType;
		char szSession [64];
		REGISTER_PARAMETER()
		{
			nType = 0;
			nStatus = 0;
			nDealType = 0;
			memset(szSession, 0, 64);
		}
		~REGISTER_PARAMETER() {}
	};

	struct UNREGISTER_PARAMETER
	{
		char szSession[64];
		UNREGISTER_PARAMETER()
		{
			memset(szSession, 0, 64);
		}
		~UNREGISTER_PARAMETER() {}
	};

	struct UNREGISTER_RESPONSE_PARAMETER
	{
		int nResult;
		struct UNREGISTER_PARAMETER unregister_parameter;
		UNREGISTER_RESPONSE_PARAMETER() : nResult(-1) {}
		~UNREGISTER_RESPONSE_PARAMETER() {}
	};

	struct REGISTER_RESPONSE_PARAMETER
	{
		int nResult;
		struct REGISTER_PARAMETER register_parameter;
		REGISTER_RESPONSE_PARAMETER(): nResult(-1) {}
		~REGISTER_RESPONSE_PARAMETER() {}
	};

	struct FETCH_PARAMETER
	{
		int nType;
		char szSession[64];
		FETCH_PARAMETER()
		{
			nType = 0;
			memset(szSession, 0, 64);
		}
		~FETCH_PARAMETER() {}
	};

	struct FETCH_RESPONSE_PARAMETER
	{
		int nResult;
		char szSession[64];
		unsigned long ulFetchDataLen;
		unsigned char * pFetchData;
		FETCH_RESPONSE_PARAMETER()
		{
			nResult = -1;
			memset(szSession, 0, 64);
			ulFetchDataLen = 0;
			pFetchData = NULL;
		}
		~FETCH_RESPONSE_PARAMETER() {}
	};

	struct FINISH_PARAMETER
	{
		long nTaskId;
		char szSession[64];
		FINISH_PARAMETER()
		{
			nTaskId = 0;
			memset(szSession, 0, 64);
		}
		~FINISH_PARAMETER() {}
	};

	struct FINISH_RESPONSE_PARAMETER
	{
		int nResult;
		char szSession[64];
		FINISH_RESPONSE_PARAMETER()
		{
			nResult = -1;
			memset(szSession, 0, 64);
		}
		~FINISH_RESPONSE_PARAMETER() {}
	};

	struct NOTIFY_PARAMETER
	{
		char szSession[64];
		int nNotifyType;
		int nNotifyValue;
		NOTIFY_PARAMETER()
		{
			memset(szSession, 0, 64);
			nNotifyType = nNotifyValue = 0;
		}
		~NOTIFY_PARAMETER() {}
	};

	struct KEEPALIVE_PARAMETER
	{
		char szSession[64];
		KEEPALIVE_PARAMETER()
		{
			memset(szSession, 0, 64);
		}
		~KEEPALIVE_PARAMETER() {}
	};

	struct POST_PARAMETER
	{
		char szSession[64];
		int nDataLen;
		unsigned char * pData;
		POST_PARAMETER()
		{
			memset(szSession, 0, 64);
			nDataLen = 0;
			pData = NULL;
		}
		~POST_PARAMETER() {}
	};

	struct ONLINE_PARAMETER
	{
		int nType;
		int nStatus;
		int nDealType;
		char szSession[64];
		ONLINE_PARAMETER()
		{
			nType = 0;
			nStatus = 0;
			nDealType = 0;
			memset(szSession, 0, 64);
		}
		~ONLINE_PARAMETER() {}
	};

	struct OFFLINE_PARAMETER
	{
		char szSession[64];
		OFFLINE_PARAMETER()
		{
			memset(szSession, 0, 64);
		}
		~OFFLINE_PARAMETER() {};
	};

	typedef REGISTER_PARAMETER MODULE_INFO;

	typedef MODULE_INFO	*MODULE_INFO_PTR;

	typedef REGISTER_PARAMETER *REGISTER_PARAMETER_PTR;

	typedef REGISTER_RESPONSE_PARAMETER *REGISTER_RESPONSE_PARAMETER_PTR;

	typedef UNREGISTER_PARAMETER *UNREGISTER_PARAMETER_PTR;

	typedef UNREGISTER_RESPONSE_PARAMETER *UNREGISTER_RESPONSE_PARAMETER_PTR;

	typedef FETCH_PARAMETER *FETCH_PARAMETER_PTR;

	typedef FETCH_RESPONSE_PARAMETER *FETCH_RESPONSE_PARAMETER_PTR;

	typedef NOTIFY_PARAMETER *NOTIFY_PARAMETER_PTR;

	typedef POST_PARAMETER *POST_PARAMETER_PTR;

	typedef ONLINE_PARAMETER *ONLINE_PARAMETER_PTR;

	typedef OFFLINE_PARAMETER *OFFLINE_PARAMTER_PTR;

	struct MESSAGE_CALLBACK_PARAMETER
	{
		char szSession[64];
		fMessageCallback msgCb;
		void * userData;
	};

	typedef MESSAGE_CALLBACK_PARAMETER *MESSAGE_CALLBACK_PARAMETER_PTR;

	struct ERROR_PARAMETER
	{
		char szSession[64];
		int nErrCode;
	};

	typedef ERROR_PARAMETER *ERROR_PARAMETER_PTR;

	struct PUSH_PARAMETER
	{
		PushType nPshType;
		char szSession[64];
		unsigned long ulDataLen;
		unsigned char * pushData;
		PUSH_PARAMETER()
		{
			nPshType = PSHT_DEFAULT;
			memset(szSession, 0, 64);
			ulDataLen = 0;
			pushData = NULL;
		}
		~PUSH_PARAMETER()
		{
			if (pushData) {
				delete[] pushData;
				pushData = NULL;
			}
			ulDataLen = 0;
		}
	};

	typedef PUSH_PARAMETER *PUSH_PARAMETER_PTR;

}

#endif 